﻿/*
 * XBMC Connect - A .Net library to remotely connect to XMBC
 * Copyright (C) 2010 - 2011 Pieter De Rycke
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;

namespace JsonRpc.Schemas
{
    [SerializableAttribute()]
    [XmlTypeAttribute(AnonymousType = true)]
    [XmlRootAttribute("json", Namespace = "", IsNullable = false)]
    public class Json
    {
        private const string JsonRpcXsd = "JsonRpc.Schemas.jsonrpc.xsd";

        private static XmlSerializer serializer;
        private static XmlSchemaSet schema;

        static Json()
        {
            // Load the embedded XSD into an XmlSchemaSet
            Assembly assembly = Assembly.GetExecutingAssembly();
            
            using (Stream stream = assembly.GetManifestResourceStream(JsonRpcXsd))
            {
                XmlReader xmlReader = XmlReader.Create(stream);

                schema = new XmlSchemaSet();
                schema.Add(null, xmlReader);
            }
        }

        public Json()
        {
            ServiceInterfaces = new List<JsonServiceInterface>();
        }

        [XmlArrayAttribute("serviceInterfaces")]
        [XmlArrayItemAttribute("serviceInterface", IsNullable = false)]
        public List<JsonServiceInterface> ServiceInterfaces { get; set; }

        [XmlAttributeAttribute("targetNamespace")]
        public string TargetNamespace { get; set; }

        #region Deserialization

        public static Json LoadFromFile(string fileName)
        {
            using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                using(StreamReader sr = new StreamReader(file))
                {
                    string xmlString = sr.ReadToEnd();

                    return Deserialize(xmlString);
                }
            }
        }

        public static Json Deserialize(string xml)
        {
            using(StringReader stringReader = new StringReader(xml))
            {
                // validate the xml against the embedded XSD
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas = schema;

                using (XmlReader xmlReader = XmlReader.Create(stringReader, settings))
                {
                    return (Json)(Serializer.Deserialize(xmlReader));
                }
            }
        }

        private static XmlSerializer Serializer
        {
            get
            {
                if (serializer == null)
                    serializer = new XmlSerializer(typeof(Json));

                return serializer;
            }
        }

        #endregion
    }
}
